/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86186
 * Date: 2023-10-15
 * Time: 22:37
 */
public class Test {
    //最长回文子串
    public String longestPalindrome(String s) {
        int begin = 0, len = 0, n = s.length();
        for (int i = 0; i < n; i++) {
            int left = i, right = i;
            while (left >= 0 && right < n && s.charAt(left) == s.charAt(right)) {
                left--;
                right++;
            }

            if (right - left - 1 > len) {
                begin = left + 1;
                len = right - left - 1;
            }

            left = i;
            right = i + 1;

            while (left >= 0 && right < n && s.charAt(left) == s.charAt(right)) {
                left--;
                right++;
            }

            if (right - left - 1 > len) {
                begin = left + 1;
                len = right - left - 1;
            }
        }

        return s.substring(begin, begin + len);
    }

    //最长公共前缀
    public String longestCommonPrefix(String[] strs) {
        for (int i = 0; i < strs[0].length(); i++) {
            char ch = strs[0].charAt(i);
            for (int j = 1; j < strs.length; j++) {
                if (i >= strs[j].length() || strs[j].charAt(i) != ch) {
                    return strs[0].substring(0, i);
                }
            }
        }

        return strs[0];
    }

    //二进制求和
    public String addBinary(String a, String b) {
        StringBuilder sb = new StringBuilder();
        int cur1 = a.length() - 1, cur2 = b.length() - 1, t = 0;
        while (cur1 >= 0 || cur2 >= 0 || t != 0) {
            if (cur1 >= 0) t += a.charAt(cur1) - '0';
            if (cur2 >= 0) t += b.charAt(cur2) - '0';
            sb.append((char)(t % 2 + '0'));
            t /= 2;

            cur1--;
            cur2--;
        }

        return sb.reverse().toString();
    }
}
